package com.easyUploader.entity.dto;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyUploader.entity.dao.Bucket;
import com.easyUploader.entity.dao.FileStorage;
import com.easyUploader.mapper.BucketMapper;
import com.easyUploader.mapper.FileStorageMapper;
import com.easyUploader.mapper.UserMapper;
import com.easyUploader.property.LocalFileProperty;
import com.easyUploader.util.FileUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.security.domain.authority.CommonRole;
import com.security.event.UserDomainEventPublisher;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 大忽悠
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class UserDto {
    private Integer id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 邮箱
     */
    private String eamil;

    /**
     * 头像
     */
    private String headImg;

    /**
     * 积分
     */
    private Integer score;

    /**
     * 家目录名字
     */
    private String homeDirName;

    /**
     * 创建桶数量
     */
    private Integer bucketNum;

    /**
     * 上传文件数量
     */
    private Integer fileUploadNum;

    /**
     * 用户拥有的角色集合
     */
    private List<CommonRole> commonRoles;

    /**
     * 是否启用
     */
    private Boolean enabled;

    /**
     * 账户是否没有过期
     */
    private boolean accountNonExpired;

    /**
     * 账户是否没有被锁定
     */
    private boolean accountNonLocked;

    /**
     * 密码是否没有过期
     */
    private boolean credentialsNonExpired;

    /**
     * 用户注册时间
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    /**
     * 用户信息最后一次更新时间
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;

    /**
     * 删除当前用户上传的所有资源在数据库记录
     */
    public void delAllResourcesRecord() {
        FileStorageMapper fileStorageMapper = SpringUtil.getBean(FileStorageMapper.class);
        fileStorageMapper.delete(new QueryWrapper<FileStorage>().eq("create_by",id));
    }

    /**
     * 删除用户记录
     */
    public void delUserRecord() {
        UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
        userMapper.deleteById(id);
        SpringUtil.getBean(UserDomainEventPublisher.class).publishUserDelEvent(username);
    }

    /**
     * 删除用户创建的桶记录
     */
    public void delAllBucketRecord() {
        BucketMapper bucketMapper = SpringUtil.getBean(BucketMapper.class);
        bucketMapper.delete(new QueryWrapper<Bucket>().eq("create_by",id));
    }

    /**
     * 递归删除用户家目录下所有资源
     */
    public void delUserHomeDir() {
        LocalFileProperty localFileProperty = SpringUtil.getBean(LocalFileProperty.class);
        FileUtil.deleteFile(localFileProperty.getUserHomePath(homeDirName));
    }
}
